﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.WorldArtPrice.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.WorldArtPrice
{
    public class AdminUserGroupBL
    {
        private readonly DbHelper _db;

        public AdminUserGroupBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);
        }

        public List<AdminUserGroup> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllAdminUserGroups) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<AdminUserGroup>(oCommand);
            return list;
        }
        public int CreateAdminUserGroup(AdminUserGroup group)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminUserGroup) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@Code", group.Code));
                oCommand.Parameters.Add(new SqlParameter("@Name", group.Name));
               
               

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int AddUserToUserGroup(int userid, int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminUserGroups_User) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserID", userid));
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int AddActionToUserGroup(int actionid, int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminUserGroups_Action) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@ActionID", actionid));
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int AddMenuToUserGroup(int menuid, int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminUserGroups_Menu) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@MenuID", menuid));
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminUserGroup(AdminUserGroup group)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateAdminUserGroup) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", group.UserGroupID));
                oCommand.Parameters.Add(new SqlParameter("@Code", group.Code));
                oCommand.Parameters.Add(new SqlParameter("@Name", group.Name));
              

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminUserGroup GetByUserGroupCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByCode) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Code", code));
            var list = _db.GetList<AdminUserGroup>(oCommand);
            return list.FirstOrDefault();
        }
        public AdminUserGroup GetByUserGroupID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserGroupID", id));
            var list = _db.GetList<AdminUserGroup>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteAdminUserGroupByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM AdminUserGroups WHERE UserGroupID IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        
        //sp_AdminUserGroup_Select_By_UserID
        public List<AdminUserGroup> GetAdminUserGroupByUserID(int userid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByUserID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserID", userid));
            var list = _db.GetList<AdminUserGroup>(oCommand);
            return list;
        }
        //  sp_AdminUserGroup_Select_By_UserName
        public List<AdminUserGroup> GetAdminUserGroupByUserName(string username)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserGroupByUserName) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserName", username));
            var list = _db.GetList<AdminUserGroup>(oCommand);
            return list;
        }
        #region Delele Privileges

        public int DeleteUserInUserGroup(int userid, int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteUserInAdminUserGroups) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserID", userid));
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int DeleteActionInUserGroup(int actionid, int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteActionInAdminUserGroups) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@ActionID", actionid));
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int DeleteMenuInUserGroup(int menuid, int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteMenuInAdminUserGroups) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@MenuID", menuid));
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public int DeleteAllUsersInUserGroup(int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteAllUsersInAdminUserGroups) { CommandType = CommandType.StoredProcedure };
                
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int DeleteAllActionsInUserGroup( int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteAllActionsInAdminUserGroups) { CommandType = CommandType.StoredProcedure };
                
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int DeleteAllMenuInUserGroup( int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteAllMenuInMAdminUserGroups) { CommandType = CommandType.StoredProcedure };
                
                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public int DeleteAllPrivilegesOfUserGroup(int usergroupid)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.DeleteAllPrivilegesOfAdminUserGroups) { CommandType = CommandType.StoredProcedure };

                oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        #endregion
    }
}
